package attend.action.attend;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionContext;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import dao.Paging.PagingDAO;

import exception.BaseException;
import attend.UserContainer.User;
import attend.com.CodeMetrix;
import attend.com.Session;
import attend.dao.attend.AttendFinishDao;
import attend.dao.common.CommonDao;

/**
 * <pre>근태마감</pre>
 * @author Yang D
 * @since 2009-04-14
 */
public class AttendFinishAction extends Session {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AttendFinishAction.class);
	
	private List 				list;
	
	private String 				s_date;

	private String 				e_date;
	
	private int 				s_row;		//시작로우
	private int					e_row;		//앤드로우
	
	private int					rowCount				=	0;		//조회된 Row Count
	private int 				cur_page 				= 	1;		//Current Page Number

	private int 				rowPerPage				=	32;		//페이지당 로우수
	
	private PagingDAO			pDao;						//페이징

	private String 				ajaxFlag				=	"N";
	private String 				updateArgs;
	private String 				crud;

	private String msg;
	
	//확인여부
	private String identy_yn = "N";
	
	//마감여부
	private String finish_yn;
	
	private User user; 
	
	private int beforArgs_i;	// 배열 갯수를 위해 추가 2011.09.01
	private int afterArgs_i;	// 배열 갯수를 위해 추가 2011.09.01
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public AttendFinishAction() {}
	
	/**
	 * <pre>근태마감 메인 조건부분</pre>
	 * @return
	 * @throws BaseException
	 * @throws SQLException 
	 * @throws SQLException 
	 */
	public String attendFinish() throws BaseException, SQLException {
		
		CommonDao commonDao 	= 	new CommonDao();
		
		s_date 					= 	commonDao.getMonthOfFirstDay();				//해당 月의 1일
		e_date					=	commonDao.getDate(0);						//오늘날짜
		
		return SUCCESS;
	}


	/**
	 * <pre>근태마감 조회 리스트</pre>
	 * @return
	 * @throws BaseException
	 */
	public String attendFinishAjax() throws BaseException {
		
		AttendFinishDao afDao = new AttendFinishDao();
		//마감데이터가 없을지 새로생성함.
		//afDao.createAttendFinish();
		rowCount	=	afDao.retrieveCurFinishDTRow(s_date , e_date , identy_yn , finish_yn );
		
		pDao 		= 	new PagingDAO(rowPerPage , 10 , rowCount , cur_page);				//페이징 처리 DAO
		
		s_row 		= 	pDao.getWriting_Start();			//시작 페이지 1
	    e_row   	= 	pDao.getWriting_End();				//끝페이지
		
		list 		= 	afDao.retrieveCurFinishDT(s_row , e_row , s_date , e_date , identy_yn , finish_yn );
		
		return SUCCESS;
	}
	
	/**
	 * <pre>근태마감 INSERT UPDATE Dummy</pre>
	 * @return
	 * @throws BaseException
	 */
	public String attendFinishListUpdateInsert() throws BaseException {
		
		logger.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		
		session	=	ActionContext.getContext().getSession();
				
		user = (User) session.get("user");		
		empInfo = user.getEmpInfo();		
		logger.info(empInfo);	
		
		List<String> getData  = new ArrayList<String>();		
		String success = null;
		String [] beforArgs = updateArgs.split(",");
		String[] afterArgs = null;
		beforArgs_i = beforArgs.length;

		
		if (ajaxFlag.equals("N")) {
			return SUCCESS;
		}
		
		AttendFinishDao afDao = new AttendFinishDao();
		
		//INSERT
		if (crud.equals(CodeMetrix.INSERT)) {
			rowCount	=	afDao.retrieveFinishDT( updateArgs );
			
			if (rowCount != 0) {
				msg	=	CodeMetrix.FAILE;
				return SUCCESS;
			}
			//approv_yn=Y, rank_nm=사원, 
			//rank_no=6008, group_no=2000, 
			//position_no=SS, today=2009년 11월 3일 <b>화요일</b>, 
			//position_nm=관리자, team_nm=전략사업, 
			//team_no=2020, group_nm=전략사업부(이규정), 
			//emp_no=1, user_id=root, emp_nm=시스템관리자			
			
			success = afDao.insertFinishCheck(updateArgs); //일관완료를 막기 위해 체크 2011.09.01

			if(success != null && success != ""){
				updateArgs = success;				
				afDao.insertFinishDT( updateArgs , empInfo.get("emp_no"));
				msg = "LIMIT";
			} else {
				msg = CodeMetrix.FAILE;
			}

			afterArgs = updateArgs.split(",");
			afterArgs_i = afterArgs.length;

			
		//DELETE
		} else if (crud.equals(CodeMetrix.DELETE)) {
			
			afDao.deleteFinishDT(updateArgs);
			
		}
		
		return SUCCESS;
	}
	
	/*************************************** GETTER / SETTER *************************************************/
	
	/**
	 * @return the s_date
	 */
	public String getS_date() {
		return s_date;
	}

	/**
	 * @param s_date the s_date to set
	 */
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}

	/**
	 * @return the e_date
	 */
	public String getE_date() {
		return e_date;
	}

	/**
	 * @param e_date the e_date to set
	 */
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	
	/**
	 * @return the list
	 */
	public List getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List list) {
		this.list = list;
	}
	
	/**
	 * @return the rowCount
	 */
	public int getRowCount() {
		return rowCount;
	}

	/**
	 * @param rowCount the rowCount to set
	 */
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	/**
	 * @return the pDao
	 */
	public PagingDAO getPDao() {
		return pDao;
	}

	/**
	 * @param dao the pDao to set
	 */
	public void setPDao(PagingDAO dao) {
		pDao = dao;
	}

	/**
	 * @return the cur_page
	 */
	public int getCur_page() {
		return cur_page;
	}

	/**
	 * @param cur_page the cur_page to set
	 */
	public void setCur_page(int cur_page) {
		this.cur_page = cur_page;
	}

	/**
	 * @return the ajaxFlag
	 */
	public String getAjaxFlag() {
		return ajaxFlag;
	}

	/**
	 * @param ajaxFlag the ajaxFlag to set
	 */
	public void setAjaxFlag(String ajaxFlag) {
		this.ajaxFlag = ajaxFlag;
	}

	/**
	 * @param updateArgs the updateArgs to set
	 */
	public void setUpdateArgs(String updateArgs) {
		this.updateArgs = updateArgs;
	}

	/**
	 * @return the updateArgs
	 */
	public String getUpdateArgs() {
		return updateArgs;
	}

	/**
	 * @param crud the crud to set
	 */
	public void setCrud(String crud) {
		this.crud = crud;
	}

	/**
	 * @return the crud
	 */
	public String getCrud() {
		return crud;
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @param finish_yn the finish_yn to set
	 */
	public void setFinish_yn(String finish_yn) {
		this.finish_yn = finish_yn;
	}

	/**
	 * @return the finish_yn
	 */
	public String getFinish_yn() {
		return finish_yn;
	}

	/**
	 * @return the identy_yn
	 */
	public String getIdenty_yn() {
		return identy_yn;
	}

	/**
	 * @param identy_yn the identy_yn to set
	 */
	public void setIdenty_yn(String identy_yn) {
		this.identy_yn = identy_yn;
	}

	public int getBeforArgs_i() {
		return beforArgs_i;
	}

	public void setBeforArgs_i(int beforArgs_i) {
		this.beforArgs_i = beforArgs_i;
	}

	public int getAfterArgs_i() {
		return afterArgs_i;
	}

	public void setAfterArgs_i(int afterArgs_i) {
		this.afterArgs_i = afterArgs_i;
	}

	
	/*************************************** GETTER / SETTER *************************************************/
}
